package cn.service.productService.ProductServiceImpl;

import cn.service.productService.AttributeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.productEntity.Attribute;
import entity.Result;
import mapper.productMapper.AttributeMapper;
import mapper.productMapper.PmsProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 朱颢阳
 * @description
 * @time 2024/8/20 22:48
 */
@Service
public class AttributeServiceImpl implements AttributeService {
    @Resource
    AttributeMapper mapper;
    @Resource
    PmsProductMapper pmsProductMapper;

    @Override
    public Result selsectAll(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Attribute> page= (Page<Attribute>) mapper.selectAll();
        System.out.println(page.getTotal());
        return Result.success((int)page.getTotal(),page.getResult());
    }
    @Override
    public Result insertAttribute(Attribute attribute) {
        if(mapper.insert(attribute)>0){
            return Result.success("添加成功");
        }else{
            return Result.failed(500,"添加失败！");
        }
    }
    @Override
    public Result updataAttribute(Attribute attribute) {
        if(mapper.updateByPrimaryKeySelective(attribute)>0){
            return Result.success("修改成功");
        }else{
            return Result.failed(500,"修改失败！");
        }
    }
    @Override
    public Result deleteAttribute(Integer id) {
        if (pmsProductMapper.selectAttProduct(id)>0){
            return Result.failed(500,"该字段还有商品存在！删除失败！");
        }else {
            if (mapper.deleteByPrimaryKey(id)>0){
                return Result.success("删除成功");
            }else{
                return Result.failed(500,"删除失败！");
            }
        }
    }
    @Override
    public Result getAttributeoption() {
        return Result.success(200,mapper.getAttributeoption());
    }
}
